package com.longshihan.lifecycler;

import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;

/**
 * Created by LONGHE001.
 *
 * @time 2019/3/27 0027
 * @des
 * @function
 */

public class SpaceEditText  extends AppCompatEditText {
    private String TAG=SpaceEditText.class.getSimpleName();
    private static int sLastLength;
    private boolean delete = false;
    //输入框内容改变监听
    private TextChangeListener listener;



    public SpaceEditText(Context context) {
        super(context);
        initView();
    }

    public SpaceEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();

    }

    public SpaceEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();

    }

    private void initView() {
        addTextChangedListener(new TextWatcher() {
            private int oldLength = 0;
            private boolean isChange = true;
            private int curLength = 0;
            private int emptyNumB = 0;  //初始空格数
            private int emptyNumA = 0;  //遍历添加空格后的空格数

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                oldLength = s.length();
                Log.i(TAG, "未改变长度: " + oldLength);
                emptyNumB = 0;
                for (int i = 0; i < s.toString().length(); i++) {
                    if (s.charAt(i) == ' ') emptyNumB++;
                }
                if (listener!=null){
                    listener.beforeTextChanged();
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                curLength = s.length();
                Log.i(TAG, "当前长度: " + curLength);
                //优化处理,如果长度未改变或则改变后长度小于3就不需要添加空格
                if (curLength == oldLength || curLength <= 3) {
                    isChange = false;
                } else {
                    isChange = true;
                }
                if (listener!=null){
                    listener.onTextChanged();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (isChange) {
                    if (curLength - sLastLength < 0) {  //判断是editext中的字符串是在减少 还是在增加
                        delete = true;
                    } else {
                        delete = false;
                    }
                    sLastLength = curLength;
                    int selectIndex = getSelectionEnd();//获取光标位置
                    String content = s.toString().replaceAll(" ", "");
                    Log.i(TAG, "content:" + content);
                    StringBuffer sb = new StringBuffer(content);
                    //遍历加空格
                    int index = 1;
                    for (int i = 0; i < content.length(); i++) {
                        if (i == 2) {
                            sb.insert(i + index, " ");
                            index++;
                        } else if (i == 6) {
                            sb.insert(i + index, " ");
                            index++;
                        }
                    }
                    Log.i(TAG, "result content:" + sb.toString());
                    String result = sb.toString();
                    //遍历加空格后 如果发现最后一位是空格 就把这个空格去掉
                    if (result.endsWith(" ")) {
                        result = result.substring(0, result.length() - 1);
                    }
                    //为edittext设置处理后的字符串
                    setText(result);

                    setSelection(result.length());
                    isChange = false;
                }
                if (listener!=null){
                    listener.afterTextChanged();
                }
            }
        });
    }


    /**
     * 输入框内容回调，当输入框内容改变时会触发
     */
    public interface TextChangeListener {
        void beforeTextChanged();
        void afterTextChanged();
        void onTextChanged();

    }

    public void setTextChangeListener(TextChangeListener listener) {
        this.listener = listener;
    }
}